<template>
  <div class="search-history-container">
    <van-list>
      <van-cell title="搜索历史">
        <div v-if="isDeleteShow">
          <span @click="$emit('update-histories', [])">全部删除</span>
          &nbsp;&nbsp;
          <span @click="isDeleteShow = false">完成</span>
        </div>
        <van-icon
          v-else
          name="delete"
          @click="isDeleteShow = true"
        />
      </van-cell>
      <van-cell
        class="van-ellipsis"
        v-for="(history, index) in searchHistories"
        :key="index"
        :title="history"
        @click="onDelete(history, index)"
      >
        <van-icon name="close" v-show="isDeleteShow" />
      </van-cell>
    </van-list>
  </div>
</template>

<script>
import { setItem } from '../../utils/storage'

export default {
  name: 'searchHistory',
  props: {
    searchHistories: {
      type: Array,
      required: true
    }
  },
  data () {
    return {
      isDeleteShow: false
    }
  },
  methods: {
    onDelete (history, index) {
      if (this.isDeleteShow) {
        this.searchHistories.splice(index, 1)
        setItem('search-histories', this.searchHistories)
      } else {
        this.$emit('search', history)
      }
    }
  }
}
</script>

<style scoped>

</style>
